#!/usr/bin/python3

"""
@author: 张伟
@time: 2018/2/28 17:46
"""
import os


class Photo(object):
    dates = list()
    packs = list()

    def __init__(self, root):
        self.root = root

    def packages(self):
        self.packs = (p for p in os.listdir(self.root))

    def date(self):
        self.dates = [(str(year), str(money).zfill(2)) for year in range(2000, 2019) for money in range(1, 13)]

    def package_files(self):
        self.date()
        for p in self.packs:
            for f in os.listdir(os.path.join(self.root, p)):
                for year, money in self.dates:
                    if year + "-" + money in f or year + money in f:
                        if year + money + "." not in f or year + money + "_" not in f:
                            continue
                        dir_photo = year + "年" + money + "月"
                        dirs = os.path.join(self.root, dir_photo)
                        if os.path.exists(dirs):
                            if os.path.exists(dirs + os.path.sep + f):
                                continue
                        else:
                            os.mkdir(os.path.join(self.root, dir_photo))
                        os.rename(os.path.join(self.root, p) + os.path.sep + f, dirs + os.path.sep + f)
                        print(f)


if __name__ == '__main__':
    p = Photo(root='D:\\来自vivo Xplay5A的相册备份')
    p.packages()
    p.package_files()
